﻿/*
 * ztime.cxx
 *
 *  Created on: 2015年4月3日
 *      Author: Fifi Lyu
 */

#include "zpsoe/zalgorithm/ztime.h"
#include "zpsoe/zexception.h"
#ifdef PLATFORM_WIN32
    #ifndef INCLUDE_WINDOWS_H_FILE
        #define INCLUDE_WINDOWS_H_FILE
        #include <Windows.h>
    #endif
#else
    #include <sys/time.h>
#endif
#include <sstream>
#include <cstdio>
#include <ctime>

using std::stringstream;

namespace zpsoe {

namespace zalgorithm {

namespace ztime {
ZPSOE_SHARED_LIB_API void mwait(const time_t &sec) {
    clock_t end_wait;
    end_wait = clock() + sec * CLOCKS_PER_SEC;

    while (clock() < end_wait) {
    }
}

ZPSOE_SHARED_LIB_API void msleep(const time_t &milli_sec) {
#ifdef PLATFORM_WIN32
    Sleep(static_cast<DWORD>(milli_sec));
#else
    struct timespec req = { 0, 0 };
    time_t sec = static_cast<time_t>(milli_sec / 1000);
    const time_t milli_sec_ = milli_sec - (sec * 1000);
    req.tv_sec = sec;
    req.tv_nsec = milli_sec_ * 1000000L;

    while (nanosleep(&req, &req) == -1)
        continue;

#endif
}

ZPSOE_SHARED_LIB_API cmp_t cmp_date(const string &d1, const string &d2) {
    /*
     格式相同的字符串，可以直接比较
     C 代码：
     int ret_ = strcmp(date1, date2)
     ret_ < 0，date1 < date2
     ret_ == 0，date1 == date2
     ret_ > 0，date1 > date2
     */
    if (d1 < d2)
        return CMP_LESS;
    else if (d1 == d2)
        return CMP_EQUAL;
    else
        return CMP_GREATER;
}

} /* namespace ztime */

} /* namespace zalgorithm */

} /* namespace zpsoe */
